// SDL2_03 [OpenGL Window].nova // Minimal OpenGL ES window, just displaying a background colour. // Using namespace declarations. using library.emscripten; using library.opengl; using library.sdl2; // The application class. class SDL2_03_OpenGL_Window { // Static data members. private static SDL_Window w; private static SDL_Renderer r; private static SDL_GLContext c; // Application class's "main" function. public static void main( String[] args ) { Stream.writeLine( "SDL2_03 [OpenGL Window].nova" ); // Disable the emscripten full-screen controls. Emscripten.disableControls( ); // Setup SDL. SDL2.SDL_Init( SDL2.SDL_INIT_VIDEO ); SDL2.SDL_GL_SetAttribute( SDL2.SDL_GL_CONTEXT_MAJOR_VERSION, 2 ); SDL2.SDL_GL_SetAttribute( SDL2.SDL_GL_CONTEXT_MINOR_VERSION, 0 ); SDL2.SDL_GL_SetAttribute( SDL2.SDL_GL_DOUBLEBUFFER, 1 ); SDL2.SDL_GL_SetAttribute( SDL2.SDL_GL_DEPTH_SIZE, 24 ); w = SDL2.SDL_CreateWindow( "SDL2_03_OpenGLWindow", SDL2.SDL_WINDOWPOS_CENTERED, SDL2.SDL_WINDOWPOS_CENTERED, 640, 480, SDL2.SDL_WINDOW_OPENGL | SDL2.SDL_WINDOW_RESIZABLE ); // Check for a null reference. if ( w == null ) { // Output an error message. Stream.writeLine( "Failed to create window: " + SDL2.SDL_GetError( ) ); // Abort the application. return; } r = SDL2.SDL_CreateRenderer( w, -1, SDL2.SDL_RENDERER_ACCELERATED | SDL2.SDL_RENDERER_PRESENTVSYNC ); c = SDL2.SDL_GL_CreateContext( w ); // Set the background colour. OpenGL.glClearColor( 0.0f, 0.5f, 0.5f, 1.0f ); // Check for the emscripten environment. if ( Emscripten.isActive( ) ) { int simulate_infinite_loop = 1; // Call the function repeatedly. int fps = -1; // Call the function as fast as the browser wants to render (typically 60fps). Emscripten.setMainLoop( renderFrame, fps, simulate_infinite_loop ); } else { // Rendering and event processing loop. do { SDL_Event e = SDL2.SDL_PollEvent( ); if ( e != null ) if ( e.id == SDL2.SDL_QUIT ) break; renderFrame( ); } while( true ); } // Shutdown app. SDL2.SDL_GL_DeleteContext( c ); SDL2.SDL_DestroyRenderer( r ); SDL2.SDL_DestroyWindow( w ); SDL2.SDL_Quit( ); } public static void renderFrame( ) { OpenGL.glClear( OpenGL.GL_COLOR_BUFFER_BIT ); SDL2.SDL_GL_SwapWindow( w ); } }